Abstract
This materiallibrary(ggplot2)
This replicates Hull (2015) example 24.7.
This is the Vasicek model (equation 24.10) in a function form.
CVaR <- function(exp, pd, r, c, l) {
v <- pnorm((qnorm(pd) + (c^0.5) * qnorm(l)) / (1 - c)^0.5)
VaR <- exp * v * (1 - r)
}
See if it works. Let’s evaluate equation 24.10 at 99% and 99.9%. The 1-year 99% and 99.9% credit VaR is:
CVaR.999 <- CVaR(100, 0.02, 0.6, 0.1, 0.999)
CVaR.99 <- CVaR(100, 0.02, 0.6, 0.1, 0.99)
CVaR.999
## [1] 5.129484
CVaR.99
## [1] 3.294271
Let’s evaluate the model at all confidence levels (from 0 to 1) simulating 10,000 values.
set.seed(13)
l <- runif(10000, 0, 1)
Loss <- CVaR(100, 0.02, 0.6, 0.1, l)
sim.var999 <- sort(Loss)[10000 * 0.999]
sim.var99 <- sort(Loss)[10000 * 0.99]
Now, visually:
hist(Loss, 100, xlim = c(0, 7), xlab = "Losses in millions", main = NULL)
abline(v = CVaR.999, lty = 2, col = "red", lwd = 2)
abline(v = CVaR.99, lty = 2, col = "blue", lwd = 2)
legend("topright", legend = c("1-year 99% credit VaR is 3.294271",
"1-year 99.9% credit VaR is 5.129484"),
col = c("blue", "red"), lwd = 2, lty = 2, bg = "white", cex = 0.8)
Figure 1.1: Distribution of losses.
dat <- data.frame(Loss, l)
ggplot(dat, aes(x = Loss, fill = "Losses in millions")) +
geom_density(color = "darkblue", fill = "lightblue") +
geom_vline(aes(xintercept = CVaR.999),
color = "red", linetype = "dashed", size = 1) +
geom_vline(aes(xintercept = CVaR.99 ),
color = "blue", linetype = "dashed", size = 1) +
scale_x_continuous(labels = scales::dollar)
Figure 1.2: Distribution of losses with ggplot.
Nice.
vector = list(range(11))
print(vector)
## [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
import numpy as np
vector = np.arange(11)
print(vector)
## [ 0 1 2 3 4 5 6 7 8 9 10]
import numpy as np
vector = np.arange(11)
print(vector)
## [ 0 1 2 3 4 5 6 7 8 9 10]
import pandas as pd
# Read the CSV file into a DataFrame
df = pd.read_csv('cr_loan2.csv')
# Access and process the data as needed
print(df.head()) # Print the first few rows of the DataFrame
## person_age ... cb_person_cred_hist_length
## 0 22 ... 3
## 1 21 ... 2
## 2 25 ... 3
## 3 23 ... 2
## 4 24 ... 4
##
## [5 rows x 12 columns]
# Check the structure of the data
print(df.dtypes)
## person_age int64
## person_income int64
## person_home_ownership object
## person_emp_length float64
## loan_intent object
## loan_grade object
## loan_amnt int64
## loan_int_rate float64
## loan_status int64
## loan_percent_income float64
## cb_person_default_on_file object
## cb_person_cred_hist_length int64
## dtype: object
# Check the first five rows of the data
pd.set_option('display.max_columns', None)
print(df.head())
## person_age person_income person_home_ownership person_emp_length
## 0 22 59000 RENT 123.0 \
## 1 21 9600 OWN 5.0
## 2 25 9600 MORTGAGE 1.0
## 3 23 65500 RENT 4.0
## 4 24 54400 RENT 8.0
##
## loan_intent loan_grade loan_amnt loan_int_rate loan_status
## 0 PERSONAL D 35000 16.02 1 \
## 1 EDUCATION B 1000 11.14 0
## 2 MEDICAL C 5500 12.87 1
## 3 MEDICAL C 35000 15.23 1
## 4 MEDICAL C 35000 14.27 1
##
## loan_percent_income cb_person_default_on_file cb_person_cred_hist_length
## 0 0.59 Y 3
## 1 0.10 N 2
## 2 0.57 N 3
## 3 0.53 N 2
## 4 0.55 Y 4
This document took 0.08 minutes to compile in Rmarkdown, R version 4.2.1 (2022-06-23 ucrt).